/**
 * Created by LUOLIANG on 2016/6/16.
 * 微信统计页面js文件
 */
var statistics = avalon.define({
    $id: "statisticsController",
    weeksum: 0,
    monsum: 0,
    monmsg: "",

    statistics: function () {
        $.ajax({
            type: "get",
            url: "/CounterDrops/weOrder/StatisticsData",
            data: {},
            success: function (data) {
                var list1 = data.weekList;
                var list2 = data.monList;
                var data1 = [0, 0, 0, 0, 0];
                var data2 = [0, 0, 0, 0, 0];
                for (var i = 0; i < list1.length; i++) {
                    statistics.weeksum += parseInt(list1[i].sum);
                    if (list1[i].ordertype == "衣服饰品") {
                        data1[0] = list1[i].sum;
                    }
                    if (list1[i].ordertype == "食品酒水") {
                        data1[1] = list1[i].sum;
                    }
                    if (list1[i].ordertype == "行车交通") {
                        data1[2] = list1[i].sum;
                    }
                    if (list1[i].ordertype == "休闲娱乐") {
                        data1[3] = list1[i].sum;
                    }
                    if (list1[i].ordertype == "其他消费") {
                        data1[4] = list1[i].sum;
                    }
                }
                for (var i = 0; i < list2.length; i++) {
                    statistics.monsum += parseInt(list2[i].sum);
                    if (list2[i].ordertype == "衣服饰品") {
                        data2[0] = list2[i].sum;
                    }
                    if (list2[i].ordertype == "食品酒水") {
                        data2[1] = list2[i].sum;
                    }
                    if (list2[i].ordertype == "行车交通") {
                        data2[2] = list2[i].sum;
                    }
                    if (list2[i].ordertype == "休闲娱乐") {
                        data2[3] = list2[i].sum;
                    }
                    if (list2[i].ordertype == "其他消费") {
                        data2[4] = list2[i].sum;
                    }
                }
                statistics.displaymsg(statistics.weeksum);
                statistics.draw(data1, 'week');
                statistics.draw(data2, 'mon');
            },
        });
    },

    displaymsg: function (data) {
        if (data >= 500) {
            statistics.monmsg = "年轻人，请注意节制，另外说一句，土豪，友乎？"
        }
        if (data < 500 && data > 300) {
            statistics.monmsg = "小伙子，你有点浪，请注意节制"
        }
        if (data < 300) {
            statistics.monmsg = "不错，你还不够浪，请继续保持"
        }
    },

    draw: function (data, type) {
        var ctx
        if (type == 'week') {
            ctx = $("#myChart");
        }
        if (type == 'mon') {
            ctx = $("#myChart1");
        }
        var myChart = new Chart(ctx, {
            type: 'pie',
            animation: {
                animateScale: true
            },
            data: {
                labels: [
                    "衣服饰品",
                    "食品酒水",
                    "行车交通",
                    "休闲娱乐",
                    "其他消费"
                ],
                datasets: [
                    {
                        data: data,
                        backgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56",
                            "#32CD32",
                            "#BFBFBF",
                        ],
                        hoverBackgroundColor: [
                            "#FF6384",
                            "#36A2EB",
                            "#FFCE56",
                            "#32CD32",
                            "#BFBFBF",
                        ]
                    }]
            },
            options: {
                scales: {
                    yAxes: [{
                        ticks: {
                            max: 5,
                            min: 0,
                            stepSize: 0.5
                        }
                    }]
                }
            }
        });
    }
});
statistics.statistics();
